<?php
namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use Carbon\Carbon;
use Spatie\ArrayToXml\ArrayToXml;
use App\Services\Facades\WebService;
class SendController extends BaseController
{
    public function send(Request $request)
    {
        $rules = [
            'orderNo' => 'bail|required',
            'logistics_no' => 'bail|required',
            'buyerName' => 'bail|required',
            'buyerTelephone' => 'bail|required',
            'ems_no' => 'bail|required',
            'province' => 'bail|required',
            'city' => 'bail|required',
            'district' => 'bail|required',
            'consigneeAddress' => 'bail|required',
            'billing_obj' => 'bail|required',
            'customer_b2b' => 'bail|required',
            'orderDTLInfo' => 'bail|required'
        ];
        $this->validate($request, $rules);
        $orderNo = $request->orderNo;
        $logistics_no = $request->logistics_no;
        $buyerName = $request->buyerName;
        $buyerTelephone = $request->buyerTelephone;
        $ems_no = $request->ems_no;
        $province = $request->province;
        $city = $request->city;
        $district = $request->district;
        $consigneeAddress = $request->consigneeAddress;
        $billing_obj = $request->billing_obj;
        $customer_b2b = $request->customer_b2b;
        $orderDTLInfo  = json_decode($request->orderDTLInfo,true);
        $array = [
            '_attributes' => ['xmlns:WMS' => 'WMS/ORDERS' , 'xmlns:xsi'=> 'http://www.w3.org/2001/XMLSchema-instance'],
            'WMS:Header'  => [
                'MESSAGE_ID'    => $orderNo,
                'MESSAGE_TYPE'  => '02',
                'VERSION'       => '1.0',
                'SENDER'        => 'ebpCode',
                'RECEIVER'      => '5151W6910G',
                'SEND_DATE'     => Carbon::now()->toDateTimeString(),
                
            ],
            'WMS:OrderBody' => [
                'OrderHead' => [
                    'ORDER_TYPE'    => 'E',
                    
                    'CUSTOMER_ROLE' => '1',
                    'CUSTOMER_B2B'  => $customer_b2b,
                    'CUSTOMER_JOB_NO'   => $orderNo,
                    'OP_FLAG'       => '1',
                ],
                'OrderInfo' => [
                    'WH_CODE'   => 'A01',
                    'BUSINESS_TYPE' => 'O901',
                    'BG_MODE'   => '3',
                    'OP_BG_MODEL'   => '2',
                    'IS_BS' => 'Y',
                    'SHIPPER_CONSIGNEE' => 'B2C',
                    'BILLING_OBJ'   => $billing_obj,
                    'EXPECTED_PIECES'   => 1,
                    'PIECES_UNIT'   => 'BOX',
                    'CURRENCY'  => 'CNY',
                    'DAMAGE_FLAG'   => 'N',
                    'BL_NO' => $logistics_no,
                    'EXTERNAL_NO1'  => '',
                    'EMS_NO'    => $ems_no,
                    'EXTERNAL_KEY'  => $orderNo,
                   // 'EXTERNAL_NO1'  => '123213219',
                ],
                'OrderDTL'  => [
                    'OrderDTLInfo'  => $orderDTLInfo
                ],
                'OrderDeliver'  => [
                    'EXPRESS'   => '510198998J',
                    'EXPRESS_NO'=> $logistics_no,
                    'DELIVERY_PERSON'   => $buyerName,
                    'DELIVERY_TEL'   => $buyerTelephone,
                    'DELIVERY_ADDR1' => $province,
                    'DELIVERY_ADDR2' => $city,
                    'DELIVERY_ADDR3' => $district,
                    'DELIVERY_ADDR4' => $consigneeAddress,

                ]
            ],
        ];
        $xml = ArrayToXml::convert($array,['rootElementName'=>'WMS:ORDERS'],false,'UTF-8','1.0');
        logger($xml);
        $params['strXml']  = $xml;
        $result = json_encode(WebService::orderPush($params));
        logger($result);
        return $result;
    }

}
